package com.zhou.system.management.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zhou.common.lib.model.CommonPage;
import com.zhou.common.lib.model.Response;
import com.zhou.common.operator.log.core.annotation.OperateLog;
import com.zhou.common.operator.log.core.constant.OperateActionEnum;
import com.zhou.system.management.constant.UserContext;
import com.zhou.system.management.entity.SysDept;
import com.zhou.system.management.entity.SysUser;
import com.zhou.system.management.entity.SysUserChangePwdAo;
import com.zhou.system.management.model.ao.RestPwdAo;
import com.zhou.system.management.model.ao.SysUserAddAo;
import com.zhou.system.management.model.ao.SysUserEnableAo;
import com.zhou.system.management.model.ao.SysUserQueryAo;
import com.zhou.system.management.model.ao.SysUserUpdateAo;
import com.zhou.system.management.model.vo.DeptRoleVo;
import com.zhou.system.management.model.vo.UserRouteVo;
import com.zhou.system.management.security.model.MyUserDetails;
import com.zhou.system.management.service.impl.SysDeptServiceImpl;
import com.zhou.system.management.service.impl.SysUserServiceImpl;
import com.zhou.system.management.validgroup.BaseGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xianfeng
 * @date 2023/5/18 22:17
 */
@Api(tags = "用户模块")
@ApiSupport(order = 7)
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl userService;

    @Autowired
    private SysDeptServiceImpl deptService;


    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询当前用户的部门")
    @PostMapping("/dept/query")
    public Response<List<SysDept>> queryUserDepts() {
        MyUserDetails myUserDetails = UserContext.currentUser();
        List<SysDept> sysDepts = deptService.queryUserDepts(myUserDetails.getUserId());
        return Response.success(sysDepts);
    }

    @OperateLog(action = OperateActionEnum.LOGIN,
            operateInstruction = "#{#username}切换部门#{#exception==null?(#resp.code==0?'成功':'失败'+#resp.msg):'失败'+#exception.message}",
            operateSystem = "shiro-test",
            operateType = "operateType",
            operateModule = "shiro"
    )
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "设置当前用户的部门")
    @PostMapping("/dept/{deptId}")
    public Response setUserCurrDept(@PathVariable Long deptId) {
        deptService.setUserCurrDept(deptId);
        return Response.success();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "查询当前用户的菜单")
    @PostMapping("/routes")
    public Response<UserRouteVo> queryUserRoute() {
        UserRouteVo userRouteVo = deptService.queryUserRoute();
        return Response.success(userRouteVo);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "查询当前部门下的用户")
    @PostMapping("/query")
    public Response<CommonPage<SysUser>> queryUsers(@RequestBody SysUserQueryAo queryAo) {
        CommonPage<SysUser> sysUsers = userService.queryUsers(queryAo);
        return Response.success(sysUsers);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "重置用户密码")
    @PostMapping("/restPwd")
    public Response restPwd(@RequestBody RestPwdAo ao) {
        userService.restPwd(ao);
        return Response.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public Response addUser(@Validated @RequestBody SysUserAddAo ao) {
        String pwd = userService.addUser(ao);
        return Response.success(pwd);
    }

    @ApiOperationSupport(order = 7, ignoreParameters = {"ao.userName", "ao.password"})
    @ApiOperation(value = "更新用户")
    @PostMapping("/update")
    public Response updateUser(@Validated(BaseGroup.UpdateGroup.class) @RequestBody SysUserUpdateAo ao) {
        ao.setUserName(null);
        userService.updateUser(ao);
        return Response.success();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{userId}")
    public Response<String> delete(@PathVariable Long userId) {
        userService.delete(userId);
        return Response.success();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询用户组织信息")
    @GetMapping("/queryDeptRole/{userId}")
    public Response<List<DeptRoleVo>> queryDeptUser(@PathVariable Long userId) {
        List<DeptRoleVo> deptRoleVos = userService.queryDeptRoles(userId);
        return Response.success(deptRoleVos);
    }


    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "启用用户")
    @PostMapping("/enableUser")
    public Response enableUser(@Validated @RequestBody SysUserEnableAo ao) {
        userService.enableUser(ao);
        return Response.success();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "启用用户")
    @PostMapping("/currUser")
    public Response<MyUserDetails> enableUser() {
        MyUserDetails myUserDetails = UserContext.currentUser();
        return Response.success(myUserDetails);
    }


    @ApiOperationSupport(order = 9, ignoreParameters = {"ao.userName", "ao.password"})
    @ApiOperation(value = "修改密码")
    @PostMapping("/changePwd")
    public Response changePwd(@Validated @RequestBody SysUserChangePwdAo ao) {
        userService.changePwd(ao);
        return Response.success();
    }

    @ApiOperationSupport(order = 9, ignoreParameters = {"ao.userName", "ao.password"})
    @ApiOperation(value = "修改基本信息")
    @PostMapping("/modifyBasicInfo")
    public Response modifyBasicInfo(@Validated(BaseGroup.UpdateGroup.class) @RequestBody SysUserUpdateAo ao) {
        ao.setUserName(null);
        userService.modifyBasicInfo(ao);
        return Response.success();
    }


}
